<template>
  <div class="book-display-container">
    <h2>搜索结果</h2>
    <!-- 书籍列表 -->
    <el-row :gutter="20">
      <el-col
        :span="24"
        v-for="book in paginatedBooks"
        :key="book.id"
        class="book-col"
      >
        <el-card shadow="hover" class="book-card" @click="$emit('navigateToBookDetail', book.id)">
          <div class="book-content">
            <img :src="book.img_url" class="book-cover" alt="书本封面" />
            <div class="book-info">
              <h3>{{ book.name }}</h3>
              <p>作者: {{ book.auther }}</p>
              <p>分类: {{ book.category }}</p>
              <p>评分: <el-rate :model-value="book.stars" disabled text-color="#ff9900" /></p>
              <p>浏览量: {{ book.view_sum }}</p>
              <p>简介: {{ book.content }}</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 分页 --><el-button @click="$emit('navigateTorecommend')">返回书单列表</el-button>
    <el-pagination
      background
      layout="prev, pager, next, total"
      :total="books.length"
      :page-size="pageSize"
      @current-change="handlePageChange"
      class="pagination"
    />
  </div>
</template>

<script setup>
import { ref, defineProps, onMounted,getCurrentInstance,watch} from "vue";
const { proxy } = getCurrentInstance();
// 接收来自父组件传递的书籍数组
const { books } = defineProps({
  books: {
    type: Array,
    default: () => [],
  },
});


const currentPage = ref(1); // 当前页
const pageSize = ref(5); // 每页显示的书籍数量
const paginatedBooks = ref([]); // 当前页的数据

// 切换页码时更新当前页的数据
const handlePageChange = (page) => {
  currentPage.value = page;
  updatePaginatedBooks();
};

// 根据当前页码更新分页数据
const updatePaginatedBooks = () => {
  const startIndex = (currentPage.value - 1) * pageSize.value;
  const endIndex = startIndex + pageSize.value;
  paginatedBooks.value = books.slice(startIndex, endIndex); // 这里依赖 props 的 books
};


watch(
  () => books,
  () => {
    currentPage.value = 1; // 每次更新时重置到第一页
    updatePaginatedBooks(); // 更新分页数据
  },
  { immediate: true } // 初始化时立即执行
);
// 初始化分页数据
onMounted(() => {
  updatePaginatedBooks();
});

// 点击跳转到书本详情
const handleCardClick = async (book) => {
  try {
    const add_view = await proxy.$axios.get(`/recommend_add/${book.id}`);
    proxy.$emit('navigateToRecomments', book);
  } catch (error) {
    console.error("Failed to add view:", error);
  }
};
</script>

<style scoped>
.book-display-container {
  padding: 20px;
  background: #f7f7f7;
  border-radius: 8px;
}

.book-col {
  margin-bottom: 20px;
}

.book-card {
  display: flex;
  align-items: flex-start;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
}

.book-card:hover {
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  transform: scale(1.02);
  transition: all 0.3s;
}

.book-content {
  display: flex;
  gap: 15px;
}

.book-cover {
  width: 120px;
  height: 160px;
  object-fit: cover;
  border-radius: 8px;
}

.book-info {
  flex: 1;
}

.book-info h3 {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.book-info p {
  font-size: 14px;
  color: #666;
  margin-bottom: 6px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
